package com.bt.framework.base

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.util.Linkify
import android.view.View
import android.view.animation.AlphaAnimation
import android.widget.Checkable
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RatingBar
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.collection.SparseArrayCompat
import androidx.recyclerview.widget.RecyclerView

/**
 *ViewHolder基类
 */
open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val mConvertView: View = itemView
    private val mViews: SparseArrayCompat<View?> = SparseArrayCompat()

    /**
     * 通过viewId获取控件
     *
     * @param viewResId
     * @return
     */
    fun <T : View?> getView(viewResId: Int): T? {
        var view = mViews[viewResId]
        if (view == null) {
            view = mConvertView.findViewById(viewResId)
            mViews.put(viewResId, view)
        }
        return view as T?
    }

    /**
     * 获取ImageView
     *
     * @param viewResId
     * @return
     */
    fun getImageView(@IdRes viewResId: Int): ImageView {
        return getView<ImageView>(viewResId)!!
    }

    /**
     * 获取TextView
     *
     * @param viewResId
     * @return
     */
    fun getTextView(@IdRes viewResId: Int): TextView {
        return getView<TextView>(viewResId)!!
    }

    /**
     * 设置控件可见性
     */
    fun setVisibility(@IdRes viewResId: Int, visibility: Int): BaseViewHolder {
        val view = getView<View>(viewResId)!!
        view.visibility = visibility
        return this
    }

    /**
     * 设置TextView内容
     */
    fun setText(@IdRes viewResId: Int, text: String?): BaseViewHolder {
        val view = getView<TextView>(viewResId)!!
        view.text = text ?: ""
        return this
    }

    /**
     * 设置点击事件
     */
    fun setOnClickListener(@IdRes viewResId: Int, listener: View.OnClickListener?): BaseViewHolder {
        getView<View>(viewResId)!!.setOnClickListener(listener)
        return this
    }

    /**
     * 设置图片资源
     */
    fun setImageResource(@IdRes viewResId: Int, @DrawableRes drawAbleResId: Int): BaseViewHolder {
        getImageView(viewResId).setImageResource(drawAbleResId)
        return this
    }

    /**
     * 显示图片
     */
    fun setImageBitmap(@IdRes viewResId: Int, bitmap: Bitmap?): BaseViewHolder {
        getImageView(viewResId).setImageBitmap(bitmap)
        return this
    }

    /**
     * 显示图片
     */
    fun setImageDrawable(@IdRes viewResId: Int, drawable: Drawable?): BaseViewHolder {
        getImageView(viewResId).setImageDrawable(drawable)
        return this
    }

    /**
     * 设置背景颜色
     */
    fun setBackgroundColor(@IdRes viewResId: Int, @ColorInt color: Int): BaseViewHolder {
        getView<View>(viewResId)!!.setBackgroundColor(color)
        return this
    }

    /**
     * 设置背景图片
     */
    fun setBackgroundResource(
        @IdRes viewResId: Int,
        @DrawableRes drawAbleResId: Int
    ): BaseViewHolder {
        getView<View>(viewResId)!!.setBackgroundResource(drawAbleResId)
        return this
    }

    /**
     * 设置文字颜色
     */
    fun setTextColor(@IdRes viewResId: Int, @ColorInt color: Int): BaseViewHolder {
        getTextView(viewResId).setTextColor(color)
        return this
    }

    /**
     * 设置透明度
     */
    @SuppressLint("ObsoleteSdkInt")
    fun setAlpha(@IdRes viewResId: Int, alpha: Float): BaseViewHolder {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getView<View>(viewResId)!!.alpha = alpha
        } else {
            val animation = AlphaAnimation(alpha, alpha).apply {
                duration = 0
                fillAfter = true
            }
            getView<View>(viewResId)!!.startAnimation(animation)
        }
        return this
    }

    /**
     * 链接化
     */
    fun linkify(@IdRes viewRsId: Int): BaseViewHolder {
        val textView = getTextView(viewRsId)
        Linkify.addLinks(textView, Linkify.ALL)
        return this
    }

    /**
     * 设置字体样式
     */
    fun setTypeface(typeface: Typeface?, vararg viewIds: Int): BaseViewHolder {
        viewIds.forEach {
            getView<TextView>(it)!!.run {
                this.typeface = typeface
                this.paintFlags = this.paintFlags or Paint.SUBPIXEL_TEXT_FLAG
            }
        }
        return this
    }

    /**
     * 设置进度
     */
    fun setProgress(viewId: Int, progress: Int): BaseViewHolder {
        getView<ProgressBar>(viewId)!!.progress = progress
        return this
    }

    /**
     * 设置进度和最大进度
     */
    fun setProgress(viewId: Int, progress: Int, max: Int): BaseViewHolder {
        getView<ProgressBar>(viewId)!!.run {
            this.max = max
            this.progress = progress
        }
        return this
    }

    /**
     * 设置最大进度
     */
    fun setMax(viewId: Int, max: Int): BaseViewHolder {
        getView<ProgressBar>(viewId)!!.max = max
        return this
    }

    /**
     * 设置评分
     */
    fun setRating(viewId: Int, rating: Float): BaseViewHolder {
        getView<RatingBar>(viewId)!!.rating = rating
        return this
    }

    /**
     * 设置Tag
     */
    fun setTag(viewId: Int, tag: Any?): BaseViewHolder {
        getView<View>(viewId)!!.tag = tag
        return this
    }

    /**
     * 设置Tag
     */
    fun setTag(viewId: Int, key: Int, tag: Any?): BaseViewHolder {
        getView<View>(viewId)!!.setTag(key, tag)
        return this
    }

    /**
     * 设置Checked
     */
    fun setChecked(viewId: Int, isCheck: Boolean): BaseViewHolder {
        val view = getView<View>(viewId) as Checkable
        view.isChecked = isCheck
        return this
    }

    /**
     * 设置选中
     */
    fun setSelected(viewId: Int, isSelect: Boolean): BaseViewHolder {
        val view = getView<View>(viewId)!!
        view.isSelected = isSelect
        return this
    }

    /**
     * 设置触摸监听
     */
    fun setOnTouchListener(viewId: Int, listener: View.OnTouchListener): BaseViewHolder {
        getView<View>(viewId)!!.setOnTouchListener(listener)
        return this
    }

    /**
     * 添加长按监听
     */
    fun setOnLongClickListener(viewId: Int, listener: View.OnLongClickListener): BaseViewHolder {
        getView<View>(viewId)!!.setOnLongClickListener(listener)
        return this
    }

}

